<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisble" :before-close="handleClose" :close-on-click-modal=false
               width="35%">
      <el-form :model="form" label-position="left" label-width="100px" ref="form" :rules="rules">
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.name" autocomplete="off" maxlength="50" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="项目描述" prop="description">
          <el-input type="textarea" v-model="form.description" autocomplete="off" maxlength="200" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="启用状态" prop="status">
          <template slot-scope="scope">
            <el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
          </template>
        </el-form-item>
        <el-form-item label="项目成员" prop="projectMember">
          <el-select v-model="form.projectMember" multiple collapse-tags style="margin-left: 20px;" placeholder="请选择项目成员" filterable>
            <el-option
              v-for="item in options"
              :key="item.userId"
              :label="item.nickname"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
export default {
  name: "cuDialog",
  props: ["dialogVisble", "title", "editData"],
  data() {
    return {
      form: {
        name: "",
        description: "",
        status: "1",
        projectMember:[]
      },
      options: [],
      rules: {
        name: [{required: true, message: '项目名称不能为空', trigger: 'blur'}],
        description: [{required: true, message: '项目描述不能为空', trigger: 'blur'}],
        status: [{required: true, message: '请选择启用状态', trigger: 'change'}],
        projectMember: [{required: true, message: '请选择项目成员', trigger: 'change'}],
      }
    }
  },
  mounted() {
    this.getUserList()
  },
  methods: {
    handleClose() {
      this.$emit("closeDialog")
    },
    getUserList(){
      this.api({url: "/user/list?pageNum=1&pageRow=999", method: "get"}).then(resp => {
        this.options = resp.list
      })
    },
    save() {
      console.log("form", this.form)
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.title === "新建项目") {
            this.form.createUser = this.$store.state.user.userId
            this.form.projectMember = JSON.stringify(this.form.projectMember)
            this.api({url: "/addProject", method: "post", data: this.form}).then(resp => {
              this.$emit("refreshList")
              this.$message({message: resp, type: 'success'});
            })
          } else {
            this.form.updateUser = this.$store.state.user.userId
            this.form.projectMember = JSON.stringify(this.form.projectMember)
            this.api({url: "/updateProject", method: "put", data: this.form}).then(resp => {
              this.$emit("refreshList")
              this.$message({message: resp, type: 'success'});
            })
          }
        }
      })

    }
  },
  watch: {
    editData() {
      if(this.editData.id){
        this.form = {...this.editData}
        this.form.projectMember = JSON.parse(this.form.projectMember)
      }else{
        this.form = {
          name: "",
          description: "",
          status: "1",
          projectMember:[]
        }
      }
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    }
  }
}
</script>

<style scoped>

</style>
